package com.android.vspice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TableRow.LayoutParams;

public class AnalysisSetting extends Activity {
	Spinner spinType;
	TableLayout layoutTable;
	Button btnOK;
	Button btnCancel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting);
		
		layoutTable = (TableLayout)findViewById(R.id.layout_table);
		spinType = (Spinner)findViewById(R.id.spinner_type);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.types, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		spinType.setAdapter(adapter);
		spinType.setOnItemSelectedListener(new SpinnerItemListener());
		
		btnOK = (Button)findViewById(R.id.button_ok);
		
		btnOK.setOnClickListener(listenOK);
		
		btnCancel = (Button)findViewById(R.id.button_cancel);
		btnCancel.setOnClickListener(listenCancel);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private void makeParamRow(int pos) {
		int count = layoutTable.getChildCount();
		
		if(count >= 2) {
			layoutTable.removeViews(1, count - 1);
		}
		
		if(pos == 0) {
			// DC
			final int rows = 4;
			String[] strText = {"Source Name", "VSTART", "VSTOP", "VINCR"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}
		}
		else if(pos == 1) {
			// TRAN
			final int rows = 4;
			String[] strText = {"TSTEP", "TSTOP", "<TSTART>", "<TMAX>"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}			
		}
	}
	
	private boolean checkParam() {
		int type = spinType.getSelectedItemPosition();
		
		if(type == 0) {
			int count = layoutTable.getChildCount();
			TableRow row = (TableRow)layoutTable.getChildAt(1);
			
		} else if(type == 1) {
			
		}
		return true;
	}
	
	private class SpinnerItemListener implements OnItemSelectedListener 
	{
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			makeParamRow(arg2);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	}
	
	private OnClickListener listenOK = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			/*
			if(!checkParam())
			makeDescription();
			*/
			
			Intent i = new Intent(AnalysisSetting.this, GraphResult.class);
			
			startActivity(i);
		}
	};
	
	private OnClickListener listenCancel = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finish();
		}
	};
}
